import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from './views/Layout.vue'
import aa from './views/aa.vue'
import ab from './views/ab.vue'
import ba from './views/ba.vue'
import bb from './views/bb.vue'
import ca from './views/ca.vue'
import da from './views/da.vue'

const routes = [
    {
        path: '/',
        component: Layout,
        meta: { title: "" }, // 面包屑显示的名称
        redirect: '/N1/i1', // 默认重定向到aa页面
        children: [
            // N1
            {
                path: 'N1',
                meta: { title: "N1" },
                redirect: '/N1/i1', // 默认重定向到第一个子项
                children: [
                    { 
                        path: 'i1', 
                        component: aa,
                        meta: { title: "ai1" } 
                    },
                    { 
                        path: 'i2', 
                        component: ab,
                        meta: { title: "ai2" } 
                    }
                ]
            },
            // N2
            {
                path: 'N2',
                meta: { title: "N2" },
                redirect: '/N2/i1',
                children: [
                    { 
                        path: 'i1', 
                        component: ba,
                        meta: { title: "bi1" } 
                    },
                    { 
                        path: 'i2', 
                        component: bb,
                        meta: { title: "bi2" } 
                    }
                ]
            },
            // N3
            {
                path: 'N3',
                meta: { title: "N3" },
                component: ca
            },
            // N4
            {
                path: 'N4',
                meta: { title: "N4" },
                component: da
            },
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 重定向逻辑
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next('/N1/i1') // 直接重定向到/N1/i1页面
  } else {
    next()
  }
})

export default router